﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using TaoPortal.BusinessLogic.DTO;
using TaoPortal.BusinessLogic.Extensions;
using TaoPortal.BusinessLogic.Repository;
using TaoPortal.Common;
using TaoPortal.Entity;


namespace TaoPortal.Areas.Admin.Models
{
    public class FileManageModel : BaseModel
    {
        public FileSearcher Searcher { get; set; }
        public string FileClass { get; set; }
        public string DomainUrl { get; set; }


        public FileManageModel()
        {
            Searcher = new FileSearcher();
            FileClass = "[]";
        }

        public List<TreeData> GetClass(string parentid)
        {
            return GetClassByPID(TaoPortal.Common.SysEnum.SysClassType.FileType, parentid);
        }

        public void InitClass()
        {
            var classList = GetClassByType(TaoPortal.Common.SysEnum.SysClassType.FileType);
            FileClass = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(classList);
        }

        public PagedList<SimpleFile> GetFilePageList()
        {
            var rp = new FileRepository();
            var data = rp.GetFileList(Searcher.FClassID, Searcher.FName, Searcher.StartDate, Searcher.EndDate, PageIndex, PageSize);
            return data;
        }

        public void Delete(string id)
        {
            var rp = new FileRepository();
            rp.DeleteFile(id);
        }

        public TP_SysFile Download(string id)
        {
            var rp = new FileRepository();
            return rp.GetFile(id);
        }
    }

    public class FileSearcher
    {
        public string FName { get; set; }
        public string FClassID { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
    }

    public class UploadModel : BaseModel
    {
        public string ClassData { get; set; }
        [Required]
        public string FileClassID { get; set; }
        [Required]
        public string FileUrl { get; set; }
        [Required]
        public string FileName { get; set; }
        public string Remark { get; set; }
        [Required]
        public int Sort { get; set; }
        public byte[] FData { get; set; }
        public int FLength { get; set; }
        public string FName { get; set; }
        public string FExtName { get; set; }
        public string FType { get; set; }
        public string Source { get; set; }
        public string ID { get; set; }

        public void InitData()
        {
            var classList = GetClassByType(TaoPortal.Common.SysEnum.SysClassType.FileType);
            ClassData = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(classList);
            FileClassID = (classList != null && classList.FirstOrDefault() != null) ? classList.FirstOrDefault().id : "";
        }

        public void InitArticleImg(string articleID, string id)
        {

        }

        public string SaveFile()
        {
            string id = Guid.NewGuid().ToString();
            var rp = new FileRepository();
            rp.SaveFile(new TP_SysFile
            {
                ID = id,
                CreateBy = SessionEnt.UserName,
                CreateTime = DateTime.Now,
                FClassID = FileClassID,
                FData = FData,
                Sort = Sort,
                Remark = Remark,
                FType = FType,
                FLength = FLength,
                FName = string.IsNullOrEmpty(FileName) ? FName : FileName,
                FExtName = FExtName
            });
            return id;
        }
        /// <summary>
        /// 更新文章图片
        /// </summary>
        /// <param name="articleID">文章ID</param>
        /// <param name="id">图片ID</param>
        /// <param name="newFile">是否重新上传图片</param>
        /// <returns>图片ID</returns>
        public string EditArticleImg(string articleID, string id, bool newFile)
        {
            if (newFile)
            {
                string _id = Guid.NewGuid().ToString();
                var rp = new FileRepository();
                rp.SaveFile(new TP_SysFile
                {
                    ID = _id,
                    CreateBy = SessionEnt.UserName,
                    CreateTime = DateTime.Now,
                    FClassID = FileClassID,
                    FData = FData,
                    Sort = Sort,
                    Remark = Remark,
                    FType = FType,
                    FLength = FLength,
                    FName = string.IsNullOrEmpty(FileName) ? FName : FileName,
                    FExtName = FExtName
                });
                return _id;
            }
            else
            {
                return id;
            }
        }

    }

    public class FileClassModel : BaseModel
    {
        public string ID { get; set; }
        public string ClassType { get; set; }
        [Required]
        [Display(Name = "分类名称")]
        public string ClassName { get; set; }
        public string ParentID { get; set; }
        public string ParentClassName { get; set; }
        [Required]
        [Display(Name = "排序")]
        public Nullable<int> Sort { get; set; }
        public string Status { get; set; }
        public string Remark { get; set; }

        public string FileClass { get; set; }

        public FileClassModel()
        {
            FileClass = "[]";
        }

        public void InitFileClass()
        {
            var classList = GetClassByType(TaoPortal.Common.SysEnum.SysClassType.FileType);
            FileClass = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(classList);
        }

        public void Add()
        {
            var rp = new SystemRepository();
            rp.AddAppClass(new TP_AppClass { ID = Guid.NewGuid().ToString(), ParentID = ParentID, ClassName = ClassName, Sort = Sort, ClassType = ((int)TaoPortal.Common.SysEnum.SysClassType.FileType).ToString() });
        }

        public void Edit()
        {
            var rp = new SystemRepository();
            rp.UpdateAppClass(new TP_AppClass
            {
                ID = this.ID,
                ParentID = this.ParentID,
                ClassName = this.ClassName,
                Sort = this.Sort
            });
        }

        public void Delete(string id)
        {
            var rp = new SystemRepository();
            rp.DeleteClass(id);
        }

        public void InitClass(string id)
        {
            var rp = new SystemRepository();
            var data = rp.GetClassByID(id);
            this.ID = data.ID;
            this.ClassName = data.ClassName;
            this.Sort = data.Sort;
            this.ParentID = data.ParentID;
        }

    }
}